package bingo.component.dhtmlxgrid.config;

import org.apache.commons.lang.StringUtils;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class ColumnConverter implements Converter {

	public void marshal(Object source, HierarchicalStreamWriter writer,
			MarshallingContext context) {
		Column column = (Column)source;
		
		writeItem(writer, "id", column.getId());
		writeItem(writer, "width", column.getWidth());
		writeItem(writer, "type", column.getType());
		writeItem(writer, "align", column.getAlign());
		writeItem(writer, "sort", column.getSort());
		writeItem(writer, "color", column.getColor());
		writeItem(writer, "hidden", String.valueOf(column.isHidden()));
		writeItem(writer, "moveable", String.valueOf(column.isMoveable()));
		writeItem(writer, "menu", String.valueOf(column.isMenu()));
		writeItem(writer, "format", column.getFormat());
		writer.setValue(column.getTitle());
		
		OptionConverter optionConverter = new OptionConverter();
		if(column.getOptions().size()>0){
			for(Option option : column.getOptions()){	
				writer.startNode("option");
				optionConverter.marshal(option, writer, context);
				writer.endNode();
			}
		}
		optionConverter = null;
	}

	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext context) {
		Column column = new Column();
		if (StringUtils.isNotEmpty(reader.getAttribute("id"))) {
			column.setId(reader.getAttribute("id"));
		}
		if (StringUtils.isNotEmpty(reader.getAttribute("width"))) {
			column.setWidth(reader.getAttribute("width"));
		}
		if (StringUtils.isNotEmpty(reader.getAttribute("type"))) {
			column.setType(reader.getAttribute("type"));
		}
		if (StringUtils.isNotEmpty(reader.getAttribute("align"))) {
			column.setAlign(reader.getAttribute("align"));
		}
		if (StringUtils.isNotEmpty(reader.getAttribute("sort"))) {
			column.setSort(reader.getAttribute("sort"));
		}
		if (StringUtils.isNotEmpty(reader.getAttribute("color"))) {
			column.setColor(reader.getAttribute("color"));
		}
		if (StringUtils.isNotEmpty(reader.getAttribute("hidden"))) {
			column.setHidden(Boolean.parseBoolean(reader.getAttribute("hidden")));
		}
		if (StringUtils.isNotEmpty(reader.getAttribute("moveable"))) {
			column.setMoveable(Boolean.parseBoolean(reader.getAttribute("moveable")));
		}
		if (StringUtils.isNotEmpty(reader.getAttribute("menu"))) {
			column.setMenu(Boolean.parseBoolean(reader.getAttribute("menu")));
		}
		if (StringUtils.isNotEmpty(reader.getAttribute("format"))) {
			column.setFormat(String.valueOf(reader.getAttribute("format")));
		}
		if (StringUtils.isNotEmpty(reader.getValue())) {
			column.setTitle(reader.getValue());
		}
		OptionConverter optionConverter = new OptionConverter();
		Option option = null;
		while(reader.hasMoreChildren()) {
			reader.moveDown();
			option = (Option)optionConverter.unmarshal(reader, context);
			column.addOption(option);
			reader.moveUp();
		}
		optionConverter = null;
		
		return column;
	}

	@SuppressWarnings("unchecked")
	public boolean canConvert(Class type) {
		return type.equals(Column.class);
	}
	
	private void writeItem(HierarchicalStreamWriter writer, String name, String value) {
		if (StringUtils.isNotEmpty(value)) {
			writer.addAttribute(name, value);
		}
	}

}
